﻿Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports System.Web.UI.Design.WebControls
Imports System.Text
Imports System.IO
Imports System.Drawing
Imports System.Web
Imports System.ComponentModel.Design
Imports System.Drawing.Design

<ToolboxBitmap(GetType(UI.WebControls.TextBox))> _
Public Class Texto
  Inherits TextBox
  Implements IScriptControl
  Implements INamingContainer


  Dim ValueValor As String = ""
  Public Property Value() As String
    Get
      Return ValueValor
    End Get
    Set(ByVal value As String)
      ValueValor = value
    End Set
  End Property

  Dim MascaraValor As String = ""
  Public Property Mascara() As String
    Get
      Return MascaraValor
    End Get
    Set(ByVal value As String)
      MascaraValor = value
    End Set
  End Property

  Dim FuncionesValor As List(Of FuncionTexto)
  <Editor(GetType(FuncionTextoCollectionEditor), GetType(UITypeEditor)), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), PersistenceMode(PersistenceMode.InnerProperty)> _
  Public ReadOnly Property Funciones() As List(Of FuncionTexto)
    Get
      If FuncionesValor Is Nothing Then
        FuncionesValor = New List(Of FuncionTexto)
      End If
      Return FuncionesValor
    End Get
  End Property


  Dim DefectoValor As String = ""
  Public Property Defecto() As String
    Get
      Return DefectoValor
    End Get
    Set(ByVal value As String)
      DefectoValor = value
    End Set
  End Property

  Dim CssErrorValor As String = "Erroneo"
  Public Property CssError() As String
    Get
      Return CssErrorValor
    End Get
    Set(ByVal value As String)
      CssErrorValor = value
    End Set
  End Property

  Dim AceptaBlancoValor As String
  Public Property AceptaBlanco() As String
    Get
      Return AceptaBlancoValor
    End Get
    Set(ByVal value As String)
      AceptaBlancoValor = value
    End Set
  End Property

  

  'Dim MensajeErrorValor As String = "Error de Formato"
  'Public Property MensajeError() As String
  '  Get
  '    Return MensajeErrorValor
  '  End Get
  '  Set(ByVal value As String)
  '    MensajeErrorValor = value
  '  End Set
  'End Property

  Dim ValidandoValor As String = ""
  Public Property Validando() As String
    Get
      Return ValidandoValor
    End Get
    Set(ByVal value As String)
      ValidandoValor = value
    End Set
  End Property

  Dim AlMenosUnoValor As Boolean = False
  Public Property AlMenosUno() As Boolean
    Get
      Return AlMenosUnoValor
    End Get
    Set(ByVal value As Boolean)
      AlMenosUnoValor = value
    End Set
  End Property


  Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
    If Not Me.DesignMode Then
      Dim SM As ScriptManager = ScriptManager.GetCurrent(Page)
      If SM Is Nothing Then
        Throw New HttpException("Debe existir un ScriptManager el la pagina")
      Else

        SM.RegisterScriptControl(Me)
      End If
    End If
    MyBase.OnPreRender(e)
  End Sub
  Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
    If Not Me.DesignMode Then
      ScriptManager.GetCurrent(Me.Page).RegisterScriptDescriptors(Me)
    End If
    If Me.DesignMode Then
      Me.Text = Me.Mascara
    Else
      Me.Text = Me.Mascara.Replace("!", " ").Replace("¡", " ").Replace("X", " ").Replace("9", " ")
    End If
    MyBase.Render(writer)
  End Sub

  Public Function GetScriptDescriptors() As IEnumerable(Of ScriptDescriptor) Implements System.Web.UI.IScriptControl.GetScriptDescriptors
    Dim descriptor As New ScriptControlDescriptor("MoniMisi2.Texto", Me.ClientID)
    Dim descriptors As New List(Of ScriptDescriptor)
    descriptor.AddProperty("Mascara", Mascara)
    descriptor.AddProperty("FuncionMascara", Funciones)
    descriptor.AddProperty("Defecto", Defecto)
    descriptor.AddProperty("CssError", CssError)
    descriptor.AddProperty("AceptaBlanco", AceptaBlanco)
    descriptor.AddProperty("AlMenosUno", AlMenosUnoValor.ToString.ToLower())

    If Me.Validando <> String.Empty Then
      descriptor.AddEvent("Validando", Me.Validando)
    End If
    descriptors.Add(descriptor)
    Return descriptors
  End Function
  Public Function GetScriptReferences() As IEnumerable(Of ScriptReference) Implements System.Web.UI.IScriptControl.GetScriptReferences
    Dim scriptRef As New ScriptReference("MoniMisi2.Texto.js", MoniMisi2.Funciones.NombreCompletoEnsamblado(Me.GetType, "MoniMisi2.Texto"))
    scriptRef.NotifyScriptLoaded = False
    Dim scriptRefs As New List(Of ScriptReference)
    scriptRefs.Add(scriptRef)
    Return scriptRefs
  End Function

  '' X = Culaquier caracter
  '' ! = Solo letras Mayusculas
  '' ¡ = Solo letras minusculas
  '' 9 = Numeros

  Public Enum TipoFuncionTexto
    AlinearDerecha = 0
    AlinearIzquierda = 1
    RellenarConCeros = 2
  End Enum


End Class
Public Class FuncionTexto
  Dim NombreValor As Texto.TipoFuncionTexto = Texto.TipoFuncionTexto.AlinearDerecha

  Public Property Nombre() As Texto.TipoFuncionTexto
    Get
      Return NombreValor
    End Get
    Set(ByVal value As Texto.TipoFuncionTexto)
      NombreValor = value
    End Set
  End Property
End Class
Public Class FuncionTextoCollectionEditor
  Inherits CollectionEditor

  Public Sub New(ByVal newType As Type)
    MyBase.new(newType)
  End Sub

  Protected Overrides Function CanSelectMultipleInstances() As Boolean
    Return False
  End Function

  Protected Overrides Function CreateCollectionItemType() As Type
    Return GetType(FuncionTexto)
  End Function

End Class
